package com.pfizer.DownLoad;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

import com.pfizer.DB.DataHelper;
import com.pfizer.Entity.UserInfo;
import com.pfizer.XmlContentHandler.SaxReadXml;
import com.pfizer.pfizerExpertForPad.PunchCardActivity;

/**
 * 異步線程類，用於處理用戶認證
 * 
 * @see android.os.AsyncTask
 * @see android.app.ProgressDialog
 * @author krui
 * @version 1.3 2011/11/22
 */
public class AsyncUserInfoXMLDownloader extends
		AsyncTask<Object, Void, UserInfo> {

	Context mContext;
	String urlStr;// XML地址
	ProgressDialog progressDialog;
	URL url = null;
	InputStream inputStream = null;
	UserInfo user;
	DataHelper dbHelper;
	boolean isConfirm = false;

	/**
	 * 構造函數
	 * 
	 * @param mContext
	 * @param urlStr
	 * @param user
	 * @param dbHelper
	 * @param isConfirm
	 */
	public AsyncUserInfoXMLDownloader(Context mContext, String urlStr,
			UserInfo user, DataHelper dbHelper, boolean isConfirm) {
		// TODO Auto-generated constructor stub
		this.mContext = mContext;
		this.urlStr = urlStr;
		this.user = user;
		this.dbHelper = dbHelper;
		this.isConfirm = isConfirm;
	}

	/**
	 * 此方法在UI線程中調用，在UI線程中顯示一個progressDialog 提示用戶後臺線程正在操作
	 */
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		progressDialog = ProgressDialog.show(mContext, null, "驗證中...", true,
				true);
	}

	/**
	 * 背端線程，處理用戶認證，認證成功存入本地數據庫，返回認證后的user
	 * @return user
	 */
	@Override
	protected UserInfo doInBackground(Object... params) {
		// TODO Auto-generated method stub
		try {
			url = new URL(urlStr);
			inputStream = url.openStream();
			
		} catch (IOException e) {
			// TODO: handle exception
			Toast.makeText(mContext, "獲取用戶信息失敗!", 1).show();
		}

		try {
			List<UserInfo> userInfoList = SaxReadXml.readUserInfo(inputStream);
			if (!userInfoList.isEmpty()) {//如果解析后的user信息非空
				user.setUserName(userInfoList.get(0).getUserName());// 将从网络上获取的user信息存到javabean
				user.setUserEmail(userInfoList.get(0).getUserEMail());
				Date date = new Date();
				String currentTime = date.toGMTString();// 得到打卡时间
				user.setLoginTime(currentTime);
				dbHelper.SaveUserInfo(user);// 存入本地数据库
			}

		} catch (Exception e) {
			// TODO: handle exception
			Toast.makeText(mContext, "同步用戶信息失敗!", 1).show();
		}
		return user;
	}
/**
 * 背端线程处理完成后，返回user将作为本方法的传入参数，此方法在UI线程中调用
 * 如果背端线程返回值非空，验证成功，进入打卡界面，否则重试
 */
	@Override
	protected void onPostExecute(UserInfo result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		progressDialog.dismiss();
		if (result != null) {
			isConfirm = true;
			Toast.makeText(mContext, "驗證成功！", Toast.LENGTH_SHORT).show();
			// 进入打卡页面
			Intent intent = new Intent(mContext, PunchCardActivity.class);
			Bundle b = new Bundle();
			b.putSerializable("UserInfo", (Serializable) user);// 将得到的登陆用户传到打卡页面
			intent.putExtras(b);
			mContext.startActivity(intent);
			((Activity) mContext).finish();// 銷毀
		} else {
			Toast.makeText(mContext, "帳號或者密碼錯誤，請重新輸入！", Toast.LENGTH_SHORT).show();
			return;
		}
	}
}
